package com.ljyanimo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljyanimo.entity.SysRole;
import com.ljyanimo.response.ServerResponse;
import com.ljyanimo.search.PageSearch;
import com.ljyanimo.service.ISysRoleService;
import com.ljyanimo.vo.SaveRoleVo;
import com.ljyanimo.vo.UpdateRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2020-10-16
 */
@Controller
@RequestMapping("/sys-role")
public class SysRoleController {

    @Autowired
    private ISysRoleService iSysRoleService;

    /**
     * 跳转后台角色列表
     * @return
     */
    @GetMapping("/listPage")
    public String listPage(){
        return "role/list";
    }

    /**
     * 跳转后台角色添加
     * @return
     */
    @GetMapping("/addPage")
    public String addPage(){
        return "role/add";
    }

    /**
     * 跳转后台角色更新
     * @return
     */
    @GetMapping("/updatePage")
    public String updatePage(){
        return "role/update";
    }


    @PostMapping("/list")
    @ResponseBody
    public ServerResponse list(@RequestBody PageSearch pageSearch){
        IPage<SysRole> iPage = new Page<>(pageSearch.getPage(),pageSearch.getLimit());

        QueryWrapper queryWrapper = new QueryWrapper();

        IPage<SysRole> page = iSysRoleService.page(iPage, queryWrapper);

        return ServerResponse.createBySuccess(page);
    }

    @PostMapping("/add")
    @ResponseBody
    public ServerResponse add(@RequestBody SaveRoleVo saveRoleVo){
        return iSysRoleService.save(saveRoleVo);
    }

    @PostMapping("/update")
    @ResponseBody
    public ServerResponse update(@RequestBody UpdateRoleVo updateRoleVo){
        return iSysRoleService.update(updateRoleVo);
    }

    @GetMapping("/one/{id}")
    @ResponseBody
    public ServerResponse one(@PathVariable("id") Long id){
        SysRole byId = iSysRoleService.getById(id);
        return ServerResponse.createBySuccess(byId);
    }

}
